import {createWebHashHistory, createRouter} from "vue-router"
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const routes = [
  {
    //聊天界面
    path: '/', 
    name: "home",
    component: () => import('@/page/home/index.vue'),
    meta: { requiresAuth: true, requiresPermission: 'chat' }
  },
  {
    // 提示词库
    path: '/prompts',
    name: 'prompts',
    component: () => import('@/page/prompts/index.vue'),
    meta: { requiresAuth: true, requiresPermission: 'prompts' }
  },
  {
    // 知识库
    path: '/knowledge',
    name: 'knowledge',
    component: () => import('@/page/knowledge/index.vue'),
    meta: { requiresAuth: true, requiresPermission: 'knowledge' }
  },
  {
    // 知识库创建向导
    path: '/knowledge/create',
    name: 'knowledge-create',
    component: () => import('@/page/knowledge/create.vue'),
    meta: { requiresAuth: true, requiresPermission: 'knowledge' }
  },
  {
    // 知识库文档列表
    path: '/knowledge/:kbId/documents',
    name: 'knowledge-documents',
    component: () => import('@/page/knowledge/documents.vue'),
    meta: { requiresAuth: true, requiresPermission: 'knowledge' }
  },
  {
    // 文档分块详情
    path: '/documents/:docId/chunks',
    name: 'document-chunks',
    component: () => import('@/page/knowledge/chunks.vue'),
    meta: { requiresAuth: true, requiresPermission: 'knowledge' }
  },
  {
    // 登录页面
    path: '/login',
    name: 'login',
    component: () => import('@/page/auth/login.vue'),
    meta: { requiresGuest: true }
  },
  {
    // 注册页面
    path: '/register',
    name: 'register',
    component: () => import('@/page/auth/register.vue'),
    meta: { requiresGuest: true }
  },
  {
    // 用户设置页面
    path: '/settings',
    name: 'settings',
    component: () => import('@/page/user/settings.vue'),
    meta: { requiresAuth: true }
  },
  {
    // 管理员页面
    path: '/admin',
    name: 'admin',
    component: () => import('@/page/admin/index.vue'),
    meta: { requiresAuth: true, requiresPermission: 'user_management' }
  }
]

//创建路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 需要登录的页面
  if (to.meta.requiresAuth) {
    if (!userStore.isLoggedIn) {
      ElMessage.warning('请先登录')
      next({ name: 'login', query: { redirect: to.fullPath } })
      return
    }
    
    // 检查权限
    if (to.meta.requiresPermission) {
      if (!userStore.hasPermission(to.meta.requiresPermission)) {
        ElMessage.error('权限不足，无法访问该页面')
        // 跳转到用户有权限的默认页面
        if (userStore.hasPermission('chat')) {
          next({ name: 'home' })
        } else if (userStore.hasPermission('prompts')) {
          next({ name: 'prompts' })
        } else if (userStore.hasPermission('knowledge')) {
          next({ name: 'knowledge' })
        } else {
          // 用户没有任何权限，强制登出
          userStore.logout()
          next({ name: 'login' })
        }
        return
      }
    }
  }
  
  // 只允许游客访问的页面（如登录、注册）
  if (to.meta.requiresGuest && userStore.isLoggedIn) {
    next({ name: 'home' })
    return
  }
  
  next()
})

export default router;
